<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/28
 * Time: 10:46
 */
namespace app\index\controller;
use app\admin\model\User;
use app\admin\model\UserInfo;
use app\admin\model\UserLevel;
use app\common\command\PhpEmail;
use app\common\controller\IndexController;
use app\Request;
use think\Validate;
use think\facade\Env;
class Login extends IndexController
{
    public function add_user(Request $request)
    {
        $all = input();
        $validate = \think\facade\Validate::rule([
            'email' => 'email|require',
            'title' => 'require',
            'code' => 'require',
            'pwd' => 'require',
        ]);
        if (!$validate->check($all)) {
            return  $this->error($validate->getError());
        }
        if(!empty(cache($all['email']))){
            if($all['code'] != cache($all['email'])){
                return  $this->error('验证码错误');
            }
        }else{
           return  $this->error('验证码不存在');
        }
        $model = new User();
        $neq_user = $model->where('email',$all['email'])->find();
        if($neq_user){
            return  $this->error('当前邮箱已被占用');
        }
        $where = [
            'email'=>$all['email'],
            'token'=>md5($all['email']),
            'title'=>$all['title'],
            'pwd'=>md5($all['pwd']),
            'superior'=>$all['superior']??0,
            'create_time'=>time(),
            'image'=>'http://yhkj-chia.oss-cn-beijing.aliyuncs.com/upload/20210615/a8760f3eb1bf067c480689928d254e9f.png'
        ];
        $res = $model->save($where);
        if($res){
            if(!empty($all['superior']) && $all['superior'] > 0){
                $sub_user = User::where(['id'=>$all['superior']])->find();
                if($sub_user){
                    UserLevel::instance()->userLevel($sub_user->toArray(),true);
                }
            }
            $type = config('cointype');
            foreach ($type as $k=>$v){
                $user_info_where=[
                    'uid'=>$model->id,
                    'type'=>$k,
                    'create_time'=>time(),
                    'update_time'=>time(),
                ];
                UserInfo::insert($user_info_where);
            }
            return  $this->success('注册成功');
        }else{
            return  $this->error('注册失败');
        }
    }

    public function login(Request $request)
    {
        $all = input();
        $validate = \think\facade\Validate::rule([
            'email' => 'email|require',
            'pwd' => 'require',
        ]);
        if (!$validate->check($all)) {
            $this->error(0,$validate->getError());
        }
        $user = new User();
        $where=[
            'email'=>$all['email'],
            'pwd'=>md5($all['pwd']),
        ];
        $data = $user->where($where)->find();
        if($data){
            $this->success('登陆成功',md5($where['email']));
        }else{
            $this->error('账号或密码错误');
        }
    }

    public function email()
    {
        $all = input();
        $num = rand(1000,9999);
        $string = "验证码：".$num;
        $res = PhpEmail::email($all['email'],$string);
        if($res['code'] == 1){
            cache($all['email'],$num,60*10);
            return  $this->success('发送成功');
        }else{
            return  $this->error('发送失败');
        }
    }

    public function add()
    {
        $superior = input('superior')??0;
        return view('login/add', ['superior' => $superior]);
    }

    public function index(){
        return view('login/index');
    }
}